package com.yidu.web.utils;

import com.yidu.web.model.WsJoke;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.select.NodeFilter;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * 爬虫类
 */
public class HTMLTools {
    public static void main(String[] args) {
       /* Parser parser = new Parser();
        try {
            //parser.setURL("http://www.qiushibaike.net/index(2005).html");
            parser.setURL("http://sina.com.cn");
            parser.setEncoding("UTF-8");

            //定义一个超链接节点的过滤器
            NodeFilter nodeFilter = new NodeClassFilter(LinkTag.class);

            NodeList list = parser.parse(nodeFilter);

            SimpleNodeIterator iterator = list.elements();

            while(iterator.hasMoreNodes()){
                Node node = iterator.nextNode();
                String text = "";
                if(node.getFirstChild()!=null){
                    text = node.getFirstChild().getText();
                }

                LinkTag link = (LinkTag)node;
                String a = link.getAttribute("href");
                String title = link.getAttribute("title");
                System.out.println(a+"   "+text);
            }
        } catch (ParserException e) {
            e.printStackTrace();
        }*/

        try {
            //jokeTitle(URL);
            String str = stringToMD5("12345678901123456789456645645623165dsadasdasfsdfsdfsdfdsfsdfdsfdsfsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsd");
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String URL = "http://www.qiushibaike.net/index(2005).html";

    public static String ROOT_URL="http://www.qiushibaike.net/"; //根网址

    /**
     * 爬笑话的标题
     */
    public static List<WsJoke> jokeTitle(String url) throws IOException {
        List<WsJoke> list = new ArrayList<>();
        Document document = Jsoup.connect(url).get();//建立连接并且得到HTML文档

        Elements elements = document.select(".Volume a[href]");

        int i = 0 ;

        for(Element element : elements){
            System.out.println(element.text()+"   "+element.attr("href"));
            String subUrl = element.attr("href");
            if(subUrl.indexOf(".html")>-1) {
                WsJoke joke = null;//找笑话内容
                try {
                    joke = jokeContent(subUrl);
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }
                joke.setTitle(element.text());
                //生成标识符
                joke.setFlag(stringToMD5(joke.getTitle()+joke.getContent()));//编码
                joke.setUrl(subUrl);
                list.add(joke);
                try {
                    Thread.sleep(520);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
        return list ;
    }

    public static WsJoke jokeContent(String url) throws IOException {

        Document document = Jsoup.connect(ROOT_URL+url).get();//建立连接并且得到HTML文档\
        WsJoke wsJoke = new WsJoke();
        //核心代码

        StringBuffer content = new StringBuffer("");
        Elements elements =        document.select(".content").select("p");

        for(Element element : elements){
            content.append(element.text());//追加
        }

        wsJoke.setContent(content.toString());



        return wsJoke;
    }

    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
}
